package cn.wolfcode.cms.web.controller;

import cn.wolfcode.cms.annotation.PermissionName;
import cn.wolfcode.cms.domain.OrderBill;
import cn.wolfcode.cms.domain.OrderBillItem;
import cn.wolfcode.cms.query.QueryObject;
import cn.wolfcode.cms.service.IOrderBillService;
import cn.wolfcode.cms.util.JSONResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

//采购订单
@Controller
@RequestMapping("orderbill")
public class OrderBillController {

    @Autowired
    private IOrderBillService orderbillService;

    @RequiresPermissions("orderbill:index")
    @PermissionName("采购订单首页")
    @RequestMapping("")
    public String index(){
        orderbillService.list();
        return "orderbill/list";
    }

    //采购订单信息列表/带分页
    @RequiresPermissions("orderbill:list")
    @PermissionName("采购订单首页")
    @RequestMapping("list")
    @ResponseBody
    public Object list(QueryObject qo){
        System.out.println(orderbillService.query(qo));
        return orderbillService.query(qo);
    }


    //所有采购订单信息数据/无分页
    @RequestMapping("listData")
    @ResponseBody
    public Object listData(){
        return orderbillService.list();
    }


    //采购订单添加
    @RequiresPermissions("orderbill:add")
    @PermissionName("采购订单添加")
    @RequestMapping("save")
    @ResponseBody
    public Object save(OrderBill entity){
        JSONResult result = new JSONResult();
        try {
            orderbillService.save(entity);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("添加失败");
        }
        return result ;
    }

    //采购订单编辑
    @RequiresPermissions("orderbill:edit")
    @PermissionName("采购订单编辑")
    @RequestMapping("update")
    @ResponseBody
    public Object edit(OrderBill entity){
        JSONResult result = new JSONResult();
        try {
            orderbillService.update(entity);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("编辑失败");
        }
        return result ;
    }

    //采购订单删除
        @RequiresPermissions("orderbill:delete")
        @PermissionName("采购订单删除")
        @RequestMapping("delete")
        @ResponseBody
        public Object delete(Long id){
            JSONResult result = new JSONResult();
            try {
                orderbillService.delete(id);
            }catch (Exception e){
                e.printStackTrace();
                result.mark("删除失败");
            }
            return result ;
    }
    //采购订单审核
        @RequiresPermissions("orderbill:audit")
        @PermissionName("采购订单审核")
        @RequestMapping("audit")
        @ResponseBody
        public Object audit(Long id){
            JSONResult result = new JSONResult();
            try {
                orderbillService.audit(id);
            }catch (Exception e){
                e.printStackTrace();
                result.mark("审核失败");
            }
            return result ;
    }


        //查询明细数据用于编辑回显
        @RequestMapping("getItemsByBillId")
        @ResponseBody
        public List<OrderBillItem> getItemsByBillId(Long id){
         List<OrderBillItem> items= orderbillService.getItemsByBillId(id);
            return items ;
    }


}
